/************************************************************************
 * <br>
 * <ul>
 * <li>版权所有：唐山弘川科技有限公司 2009－2020</li>
 * <li>创建日期：2009-8-10</li>
 * <li>初始作者：刘川</li>
 * </ul>
 * <ul>
 * <b>修订历史</b>
 * <li>1、</li>
 * </ul>
 ***********************************************************************/
package com.hy.enterprise.framework.persistence.domain.query ;

import java.util.Collection;
import java.util.Date;

import com.hy.enterprise.framework.persistence.domain.query.internal.AliasFactory;
import com.hy.enterprise.framework.persistence.domain.query.internal.LibraPersistentObjectFactory;
import com.hy.enterprise.framework.persistence.domain.query.internal.managedobjects.LibraManagedObject;

/**
 * <ul>
 * <li>设计作者：刘川</li>
 * <li>设计日期：2009-8-10</li>
 * <li>设计时间：下午04:19:08</li>
 * <li>设计目的：</li>
 * </ul>
 * <ul>
 * <b>修订历史</b>
 * <li>1、</li>
 * </ul>
 */
public class Functions
{
	/**
	 * <ul>
	 * <li>设计作者：刘川</li>
	 * <li>设计日期：2009-8-10</li>
	 * <li>设计时间：下午04:22:37</li>
	 * <li>设计目的：截取类型</li>
	 * </ul>
	 * <ul>
	 * <b>修订历史</b>
	 * <li>1、</li>
	 * </ul>
	 */
	public static enum TrimSpecification
	{
		BOTH (
		      "BOTH " ) ,
		LEADING (
		         "LEADING " ) ,
		TRAILING (
		          "TRAILING " ) ;

		private final String	stringRepresentation ;

		/**
		 * 构造函数
		 * 
		 * @param stringRepresentation
		 */
		private TrimSpecification (
		                            String stringRepresentation )
		{
			this.stringRepresentation =
			                            stringRepresentation ;
		}
	}

	public static final TrimSpecification	BOTH	 =
	                                                   TrimSpecification.BOTH ;

	public static final TrimSpecification	LEADING	 =
	                                                   TrimSpecification.LEADING ;

	public static final TrimSpecification	TRAILING	=
	                                                      TrimSpecification.TRAILING ;

	/**
	 * <ul>
	 * <li>设计作者：刘川</li>
	 * <li>设计日期：2009-8-10</li>
	 * <li>设计时间：下午04:29:39</li>
	 * <li>设计目的：</li>
	 * </ul>
	 * <ul>
	 * <b>修订历史</b>
	 * <li>1、</li>
	 * </ul>
	 * 
	 * @param <N>
	 * @param expr
	 * @return
	 */
	public static < N extends Number > N abs (
	                                           final N expr )
	{
		N index =
		          LibraPersistentObjectFactory.INSTANCE
		                                               .makeNew ( expr ) ;
		AliasFactory
		            .remember (
		                        index ,
		                        new LibraManagedObject ( )
		                        {
			                        @ Override
			                        public String getTargetRepresentation ( )
			                        {
				                        return "ABS("
				                               + AliasFactory
				                                             .safeStringForm ( expr )
				                               + ")" ;
			                        }
		                        } ) ;
		return index ;
	}

	/**
	 * <ul>
	 * <li>设计作者：刘川</li>
	 * <li>设计日期：2009-8-10</li>
	 * <li>设计时间：下午04:20:13</li>
	 * <li>设计目的：</li>
	 * </ul>
	 * <ul>
	 * <b>修订历史</b>
	 * <li>1、</li>
	 * </ul>
	 * 
	 * @param lhs
	 * @param rhs
	 * @return
	 */
	public static String concat (
	                              final String lhs ,
	                              final String rhs )
	{
		String string =
		                LibraPersistentObjectFactory.INSTANCE
		                                                     .makeNew ( String.class ) ;
		AliasFactory
		            .remember (
		                        string ,
		                        new LibraManagedObject ( )
		                        {
			                        @ Override
			                        public String getTargetRepresentation ( )
			                        {
				                        return "CONCAT("
				                               + AliasFactory
				                                             .safeStringForm ( lhs )
				                               + ", "
				                               + AliasFactory
				                                             .safeStringForm ( rhs )
				                               + ")" ;
			                        }
		                        } ) ;
		return string ;
	}

	/**
	 * <ul>
	 * <li>设计作者：刘川</li>
	 * <li>设计日期：2009-8-10</li>
	 * <li>设计时间：下午04:27:59</li>
	 * <li>设计目的：</li>
	 * </ul>
	 * <ul>
	 * <b>修订历史</b>
	 * <li>1、</li>
	 * </ul>
	 * 
	 * @return
	 */
	public static Date currentDate ( )
	{
		Date date =
		            LibraPersistentObjectFactory.INSTANCE
		                                                 .makeNew ( Date.class ) ;
		AliasFactory
		            .remember (
		                        date ,
		                        new LibraManagedObject ( )
		                        {
			                        @ Override
			                        public String getTargetRepresentation ( )
			                        {
				                        return "current_date" ;
			                        }
		                        } ) ;
		return date ;
	}

	/**
	 * <ul>
	 * <li>设计作者：刘川</li>
	 * <li>设计日期：2009-8-10</li>
	 * <li>设计时间：下午04:27:35</li>
	 * <li>设计目的：</li>
	 * </ul>
	 * <ul>
	 * <b>修订历史</b>
	 * <li>1、</li>
	 * </ul>
	 * 
	 * @return
	 */
	public static Date currentTime ( )
	{
		Date date =
		            LibraPersistentObjectFactory.INSTANCE
		                                                 .makeNew ( Date.class ) ;
		AliasFactory
		            .remember (
		                        date ,
		                        new LibraManagedObject ( )
		                        {
			                        @ Override
			                        public String getTargetRepresentation ( )
			                        {
				                        return "current_time" ;
			                        }
		                        } ) ;
		return date ;
	}

	/**
	 * <ul>
	 * <li>设计作者：刘川</li>
	 * <li>设计日期：2009-8-10</li>
	 * <li>设计时间：下午04:27:11</li>
	 * <li>设计目的：</li>
	 * </ul>
	 * <ul>
	 * <b>修订历史</b>
	 * <li>1、</li>
	 * </ul>
	 * 
	 * @return
	 */
	public static Date currentTimestamp ( )
	{
		Date date =
		            LibraPersistentObjectFactory.INSTANCE
		                                                 .makeNew ( Date.class ) ;
		AliasFactory
		            .remember (
		                        date ,
		                        new LibraManagedObject ( )
		                        {
			                        @ Override
			                        public String getTargetRepresentation ( )
			                        {
				                        return "current_timestamp" ;
			                        }
		                        } ) ;
		return date ;
	}

	/**
	 * <ul>
	 * <li>设计作者：刘川</li>
	 * <li>设计日期：2009-8-10</li>
	 * <li>设计时间：下午04:30:43</li>
	 * <li>设计目的：</li>
	 * </ul>
	 * <ul>
	 * <b>修订历史</b>
	 * <li>1、</li>
	 * </ul>
	 * 
	 * @param string
	 * @return
	 */
	public static Integer length (
	                               final String string )
	{
		Integer index =
		                LibraPersistentObjectFactory.INSTANCE
		                                                     .makeNew ( Integer.class ) ;
		AliasFactory
		            .remember (
		                        index ,
		                        new LibraManagedObject ( )
		                        {
			                        @ Override
			                        public String getTargetRepresentation ( )
			                        {
				                        return "LENGTH("
				                               + AliasFactory
				                                             .safeStringForm ( string )
				                               + ")" ;
			                        }
		                        } ) ;
		return index ;
	}

	/**
	 * <ul>
	 * <li>设计作者：刘川</li>
	 * <li>设计日期：2009-8-10</li>
	 * <li>设计时间：下午04:30:22</li>
	 * <li>设计目的：</li>
	 * </ul>
	 * <ul>
	 * <b>修订历史</b>
	 * <li>1、</li>
	 * </ul>
	 * 
	 * @param string
	 * @param in
	 * @return
	 */
	public static Integer locate (
	                               final String string ,
	                               final String in )
	{
		Integer index =
		                LibraPersistentObjectFactory.INSTANCE
		                                                     .makeNew ( Integer.class ) ;
		AliasFactory
		            .remember (
		                        index ,
		                        new LibraManagedObject ( )
		                        {
			                        @ Override
			                        public String getTargetRepresentation ( )
			                        {
				                        return "LOCATE("
				                               + AliasFactory
				                                             .safeStringForm ( string )
				                               + ", "
				                               + AliasFactory
				                                             .safeStringForm ( in )
				                               + ")" ;
			                        }
		                        } ) ;
		return index ;
	}

	/**
	 * <ul>
	 * <li>设计作者：刘川</li>
	 * <li>设计日期：2009-8-10</li>
	 * <li>设计时间：下午04:30:03</li>
	 * <li>设计目的：</li>
	 * </ul>
	 * <ul>
	 * <b>修订历史</b>
	 * <li>1、</li>
	 * </ul>
	 * 
	 * @param string
	 * @param in
	 * @param start
	 * @return
	 */
	public static Integer locate (
	                               final String string ,
	                               final String in ,
	                               final Integer start )
	{
		Integer index =
		                LibraPersistentObjectFactory.INSTANCE
		                                                     .makeNew ( Integer.class ) ;
		AliasFactory
		            .remember (
		                        index ,
		                        new LibraManagedObject ( )
		                        {
			                        @ Override
			                        public String getTargetRepresentation ( )
			                        {
				                        return "LOCATE("
				                               + AliasFactory
				                                             .safeStringForm ( string )
				                               + ", "
				                               + AliasFactory
				                                             .safeStringForm ( in )
				                               + ", "
				                               + AliasFactory
				                                             .safeStringForm ( start )
				                               + ")" ;
			                        }
		                        } ) ;
		return index ;
	}

	/**
	 * <ul>
	 * <li>设计作者：刘川</li>
	 * <li>设计日期：2009-8-10</li>
	 * <li>设计时间：下午04:26:14</li>
	 * <li>设计目的：</li>
	 * </ul>
	 * <ul>
	 * <b>修订历史</b>
	 * <li>1、</li>
	 * </ul>
	 * 
	 * @param string
	 * @return
	 */
	public static String lower (
	                             final String string )
	{
		String tempString =
		                    LibraPersistentObjectFactory.INSTANCE
		                                                         .makeNew ( String.class ) ;
		AliasFactory
		            .remember (
		                        tempString ,
		                        new LibraManagedObject ( )
		                        {
			                        @ Override
			                        public String getTargetRepresentation ( )
			                        {
				                        return "LOWER("
				                               + AliasFactory
				                                             .safeStringForm ( string )
				                               + ")" ;
			                        }
		                        } ) ;
		return tempString ;
	}

	/**
	 * <ul>
	 * <li>设计作者：刘川</li>
	 * <li>设计日期：2009-8-10</li>
	 * <li>设计时间：下午04:28:58</li>
	 * <li>设计目的：</li>
	 * </ul>
	 * <ul>
	 * <b>修订历史</b>
	 * <li>1、</li>
	 * </ul>
	 * 
	 * @param expr
	 * @param divider
	 * @return
	 */
	public static Integer mod (
	                            final Integer expr ,
	                            final Integer divider )
	{
		Integer index =
		                LibraPersistentObjectFactory.INSTANCE
		                                                     .makeNew ( Integer.class ) ;
		AliasFactory
		            .remember (
		                        index ,
		                        new LibraManagedObject ( )
		                        {
			                        @ Override
			                        public String getTargetRepresentation ( )
			                        {
				                        return "MOD("
				                               + AliasFactory
				                                             .safeStringForm ( expr )
				                               + ", "
				                               + AliasFactory
				                                             .safeStringForm ( divider )
				                               + ")" ;
			                        }
		                        } ) ;
		return index ;
	}

	/**
	 * <ul>
	 * <li>设计作者：刘川</li>
	 * <li>设计日期：2009-8-10</li>
	 * <li>设计时间：下午04:28:24</li>
	 * <li>设计目的：</li>
	 * </ul>
	 * <ul>
	 * <b>修订历史</b>
	 * <li>1、</li>
	 * </ul>
	 * 
	 * @param collection
	 * @return
	 */
	public static Integer size (
	                             final Collection < ? > collection )
	{
		Integer index =
		                LibraPersistentObjectFactory.INSTANCE
		                                                     .makeNew ( Integer.class ) ;
		AliasFactory
		            .remember (
		                        index ,
		                        new LibraManagedObject ( )
		                        {
			                        @ Override
			                        public String getTargetRepresentation ( )
			                        {
				                        return "SIZE("
				                               + AliasFactory
				                                             .safeStringForm ( collection )
				                               + ")" ;
			                        }
		                        } ) ;
		return index ;
	}

	/**
	 * <ul>
	 * <li>设计作者：刘川</li>
	 * <li>设计日期：2009-8-10</li>
	 * <li>设计时间：下午04:29:18</li>
	 * <li>设计目的：</li>
	 * </ul>
	 * <ul>
	 * <b>修订历史</b>
	 * <li>1、</li>
	 * </ul>
	 * 
	 * @param expr
	 * @return
	 */
	public static Double sqrt (
	                            final Number expr )
	{
		Double index =
		               LibraPersistentObjectFactory.INSTANCE
		                                                    .makeNew ( Double.class ) ;
		AliasFactory
		            .remember (
		                        index ,
		                        new LibraManagedObject ( )
		                        {
			                        @ Override
			                        public String getTargetRepresentation ( )
			                        {
				                        return "SQRT("
				                               + AliasFactory
				                                             .safeStringForm ( expr )
				                               + ")" ;
			                        }
		                        } ) ;
		return index ;
	}

	/**
	 * <ul>
	 * <li>设计作者：刘川</li>
	 * <li>设计日期：2009-8-10</li>
	 * <li>设计时间：下午04:20:53</li>
	 * <li>设计目的：</li>
	 * </ul>
	 * <ul>
	 * <b>修订历史</b>
	 * <li>1、</li>
	 * </ul>
	 * 
	 * @param string
	 * @param start
	 * @param length
	 * @return
	 */
	public static String substring (
	                                 final String string ,
	                                 final Integer start ,
	                                 final Integer length )
	{
		String tempString =
		                    LibraPersistentObjectFactory.INSTANCE
		                                                         .makeNew ( String.class ) ;
		AliasFactory
		            .remember (
		                        tempString ,
		                        new LibraManagedObject ( )
		                        {
			                        @ Override
			                        public String getTargetRepresentation ( )
			                        {
				                        return "SUBSTRING("
				                               + AliasFactory
				                                             .safeStringForm ( string )
				                               + ", "
				                               + AliasFactory
				                                             .safeStringForm ( start )
				                               + ", "
				                               + AliasFactory
				                                             .safeStringForm ( length )
				                               + ")" ;
			                        }
		                        } ) ;
		return tempString ;
	}

	/**
	 * <ul>
	 * <li>设计作者：刘川</li>
	 * <li>设计日期：2009-8-10</li>
	 * <li>设计时间：下午04:21:10</li>
	 * <li>设计目的：</li>
	 * </ul>
	 * <ul>
	 * <b>修订历史</b>
	 * <li>1、</li>
	 * </ul>
	 * 
	 * @param string
	 * @return
	 */
	public static String trim (
	                            final String string )
	{
		String tempString =
		                    LibraPersistentObjectFactory.INSTANCE
		                                                         .makeNew ( String.class ) ;
		AliasFactory
		            .remember (
		                        tempString ,
		                        new LibraManagedObject ( )
		                        {
			                        @ Override
			                        public String getTargetRepresentation ( )
			                        {
				                        return "TRIM("
				                               + AliasFactory
				                                             .safeStringForm ( string )
				                               + ")" ;
			                        }
		                        } ) ;
		return tempString ;
	}

	/**
	 * <ul>
	 * <li>设计作者：刘川</li>
	 * <li>设计日期：2009-8-10</li>
	 * <li>设计时间：下午04:23:35</li>
	 * <li>设计目的：</li>
	 * </ul>
	 * <ul>
	 * <b>修订历史</b>
	 * <li>1、</li>
	 * </ul>
	 * 
	 * @param string
	 * @param toTrim
	 * @return
	 */
	public static String trim (
	                            final String string ,
	                            final Character toTrim )
	{
		String tempString =
		                    LibraPersistentObjectFactory.INSTANCE
		                                                         .makeNew ( String.class ) ;
		AliasFactory
		            .remember (
		                        tempString ,
		                        new LibraManagedObject ( )
		                        {
			                        @ Override
			                        public String getTargetRepresentation ( )
			                        {
				                        return "TRIM("
				                               + AliasFactory
				                                             .safeStringForm ( toTrim )
				                               + " FROM "
				                               + AliasFactory
				                                             .safeStringForm ( string )
				                               + ")" ;
			                        }
		                        } ) ;
		return tempString ;
	}

	/**
	 * <ul>
	 * <li>设计作者：刘川</li>
	 * <li>设计日期：2009-8-10</li>
	 * <li>设计时间：下午04:24:31</li>
	 * <li>设计目的：</li>
	 * </ul>
	 * <ul>
	 * <b>修订历史</b>
	 * <li>1、</li>
	 * </ul>
	 * 
	 * @param string
	 * @param toTrim
	 * @param trimSpecification
	 * @return
	 */
	public static String trim (
	                            final String string ,
	                            final Character toTrim ,
	                            final TrimSpecification trimSpecification )
	{
		String tempString =
		                    LibraPersistentObjectFactory.INSTANCE
		                                                         .makeNew ( String.class ) ;
		AliasFactory
		            .remember (
		                        tempString ,
		                        new LibraManagedObject ( )
		                        {
			                        @ Override
			                        public String getTargetRepresentation ( )
			                        {
				                        return "TRIM("
				                               + trimSpecification.stringRepresentation
				                               + AliasFactory
				                                             .safeStringForm ( toTrim )
				                               + " FROM "
				                               + AliasFactory
				                                             .safeStringForm ( string )
				                               + ")" ;
			                        }
		                        } ) ;
		return tempString ;
	}

	/**
	 * <ul>
	 * <li>设计作者：刘川</li>
	 * <li>设计日期：2009-8-10</li>
	 * <li>设计时间：下午04:25:01</li>
	 * <li>设计目的：</li>
	 * </ul>
	 * <ul>
	 * <b>修订历史</b>
	 * <li>1、</li>
	 * </ul>
	 * 
	 * @param string
	 * @param trimSpecification
	 * @return
	 */
	public static String trim (
	                            final String string ,
	                            final TrimSpecification trimSpecification )
	{
		String tempString =
		                    LibraPersistentObjectFactory.INSTANCE
		                                                         .makeNew ( String.class ) ;
		AliasFactory
		            .remember (
		                        tempString ,
		                        new LibraManagedObject ( )
		                        {
			                        @ Override
			                        public String getTargetRepresentation ( )
			                        {
				                        return "TRIM("
				                               + trimSpecification.stringRepresentation
				                               + " FROM "
				                               + AliasFactory
				                                             .safeStringForm ( string )
				                               + ")" ;
			                        }
		                        } ) ;
		return tempString ;
	}

	/**
	 * <ul>
	 * <li>设计作者：刘川</li>
	 * <li>设计日期：2009-8-10</li>
	 * <li>设计时间：下午04:25:45</li>
	 * <li>设计目的：</li>
	 * </ul>
	 * <ul>
	 * <b>修订历史</b>
	 * <li>1、</li>
	 * </ul>
	 * 
	 * @param string
	 * @return
	 */
	public static String upper (
	                             final String string )
	{
		String tempString =
		                    LibraPersistentObjectFactory.INSTANCE
		                                                         .makeNew ( String.class ) ;
		AliasFactory
		            .remember (
		                        tempString ,
		                        new LibraManagedObject ( )
		                        {
			                        @ Override
			                        public String getTargetRepresentation ( )
			                        {
				                        return "UPPER("
				                               + AliasFactory
				                                             .safeStringForm ( string )
				                               + ")" ;
			                        }
		                        } ) ;
		return tempString ;
	}

	/**
	 * 构造函数
	 */
	private Functions ( )
	{
		super ( ) ;
	}
}
